<template>
	<view class="userinfo">
		<form @submit="save">
			<view class="info-input">
				<view class="title">
					昵称：
				</view>
				<input :value="name" type="nickname" name="name" placeholder="请输入昵称" />
			</view>
			<button class="submit" form-type="submit">保存昵称</button>
		</form>
	</view>
</template>

<script>
	import {
		loginUser,
		updateBase
	} from "@/api/api.js"
	export default {
		data() {
			return {
				name: '',
				info: [],
			}
		},
		onLoad() {
			this.UserInfo()
		},
		methods: {
			save(e) {
				let that = this
				let nickname = e.detail.value.name
				if (nickname == '') return uni.$showMsg('请输入昵称')
				updateBase({
					nickname
				}).then(res => {
					uni.$showMsg(res.msg)
					if (res.code == 200) {
						setTimeout(() => {
							let pages = getCurrentPages(); //获取所有页面栈实例列表
							let nowPage = pages[pages.length - 1]; //当前页页面实例
							let prevPage = pages[pages.length - 2]; //上一页页面实例
							prevPage.$vm.info.nickname = nickname; //修改上一页data里面的tagIndex 参数值
							uni.navigateBack({ //uni.navigateTo跳转的返回，默认1为返回上一级
								delta: 1
							});
						}, 500)
					}
				})
			},
			UserInfo() {
				loginUser().then(res => {
					if (res.code == 200) {
						this.info = res.data
						this.name = this.info.nickname
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		background: #f2f2f2;
		padding-top: 10px;
	}

	.submit {
		margin-top: 30px;

		&::after {
			border: 0;
		}
	}

	.userinfo {
		width: 96%;
		margin: 0 auto;
		border-radius: 10px;

		.top {
			text-align: center;
			padding-top: 20px;

			image {
				width: 100px;
				height: 100px;
				border-radius: 50%;
			}
		}

		.info-input {
			padding: 5%;
			font-size: 14px;
			display: flex;
			align-items: center;
			background: #fff;

			input {
				height: 40px;
				background-color: #fff;
				padding-left: 40rpx;
				font-size: 14px;
				width: 70%;
			}
		}
	}
</style>